﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using System.Transactions;
using T.FAS.Action.MetaData.Entity;
using T.FAS.Base.Dao;
using Teld.Core.DAC.Service;
using Teld.Core.DataAccessEx.Core;

namespace T.FAS.Action.Dev.Dao
{
    public class ActionMetaDataELBDao : DaoService<ActionMetaData>,IActionMetaDataDao
    {
        public const string MetaDataID = "ActionMetaData";
        public const string MetaDataName = "Action元数据";
        public override string TableName => "FAS_ActionMetaData";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName
            => DaoUtil.GetExceptionColumn<ActionMetaData>(
                new List<string>
                {
                    "MetaDataID",
                    "MetaDataCode",
                    "MetaDataName"
                }
            );

        public override ActionMetaData ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("ActionType"))
                columns.Remove("ActionType");
            if (columns.ContainsKey("ReturnValueType"))
                columns.Remove("ReturnValueType");

            var data = base.ConvertToObject(columns, row);

            data.ActionType = (ActionType)Enum.Parse(typeof(ActionType), Convert.ToString(row["ActionType"]));
            data.ReturnValueType = (ReturnValueType)Enum.Parse(typeof(ReturnValueType), Convert.ToString(row["ReturnValueType"]));

            return data;
        }

        public override Dictionary<string, object> GetDataMapping(ActionMetaData data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("ActionType");
            mapping.Add("ActionType", (int)data.ActionType);

            mapping.Remove("ReturnValueType");
            mapping.Add("ReturnValueType", (int)data.ReturnValueType);

            return mapping;
        }


        private ActionMetaDataMethodELBDao actionMetaDataMethodELBDao => new ActionMetaDataMethodELBDao();
        private ActionMetaDataMethodParamELBDao actionMetaDataMethodParamELBDao => new ActionMetaDataMethodParamELBDao();
        private ActionACLDefineELBDao actionACLDefineELBDao => new ActionACLDefineELBDao();
        private ActionACLDetailELBDao actionACLDetailELBDao => new ActionACLDetailELBDao();
        public ActionMetaDataELBDao() : base()
        {
            DBName = "TeldFAS";
        }
        public override Dictionary<string, PropertyInfo> GetColumns()
        {
            return DaoServiceExtension.GetAllColumns<ActionMetaData>(base.GetColumns());
        }
        /// <summary>
        /// 根据ActionID删除Action元数据
        /// </summary>
        /// <param name="actionID"></param>
        public void DeleteActionMetaData(string actionID)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    //删除ACL定义
                    actionACLDefineELBDao.DeleteActionACLDefineByActionID(actionID);
                    //删除ActionMetaDataMethodParam
                    actionMetaDataMethodParamELBDao.DeleteActionMetadataMethodParamByActionID(actionID);
                    //删除ActionMetaDataMethod
                    actionMetaDataMethodELBDao.DeleteActionMetadataMethodByActionID(actionID);
                    //删除ActionMetaData
                    base.Delete(new List<string> { actionID });
                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                throw new Exception($"{MetaDataName}:保存失败！", e);
            }
        }
        public List<ActionMetaData> GetActionMetaDataByFilter(Dictionary<string, string> filter)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 根据ActionID获取Action元数据信息
        /// </summary>
        /// <param name="actionID"></param>
        /// <returns></returns>
        public ActionMetaData GetActionMetaDataByID(string actionID)
        {
            var actionMetaData = base.Get(actionID);
            var action = actionMetaData as ActionMetaData;
            if (action == null)
                return null;
            action.Method = actionMetaDataMethodELBDao.GetActionMetaDataMethodByActionID(actionID);
            action.MethodParams = actionMetaDataMethodParamELBDao.GetActionMetaDataMethodParamsByActionID(actionID);
            return action;
        }
        public void UpdateActionMetaData(ActionMetaData action)
        {
            throw new NotImplementedException();
        }
    }
}
